package com.example.day0818_sever.slice;

import com.example.day0818_sever.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.IAbilityConnection;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.window.dialog.ToastDialog;
import ohos.bundle.ElementName;
import ohos.bundle.IBundleManager;
import ohos.rpc.IRemoteObject;
import ohos.security.SystemPermission;

public class MainAbilitySlice extends AbilitySlice {
    private static final String SERVICE_BUND_NAME = "com.example.day0818_sever";
    private static final String SERVICE_OBJ_NAME = "com.example.day0818_sever.ServiceAbility_one";
    private static final String SERVICE_FORBACK_NAME = "com.example.day0818_sever.ForBackgroud_ServiceAbility";
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        MonitorPackage();
    }

    private void MonitorPackage() {
        Button button1 = (Button)findComponentById(ResourceTable.Id_main_start_btn);
        Button button2 = (Button)findComponentById(ResourceTable.Id_main_stop_btn);
        Button button3 = (Button)findComponentById(ResourceTable.Id_main_connection_btn);
        Button button4 = (Button)findComponentById(ResourceTable.Id_main_disconnection_btn);
        Button button5 = (Button)findComponentById(ResourceTable.Id_main_forbackground_btn);
        Button button6 = (Button)findComponentById(ResourceTable.Id_main_stop_forback_btn);

        button1.setClickedListener(component -> startServerce(SERVICE_BUND_NAME,SERVICE_OBJ_NAME));
        button2.setClickedListener(component -> stopServerce(SERVICE_BUND_NAME,SERVICE_OBJ_NAME));
        button3.setClickedListener(component -> connectServerce(SERVICE_BUND_NAME,SERVICE_OBJ_NAME));
        button4.setClickedListener(component -> disconnectAbility(connection));
        button5.setClickedListener(component -> startServerce(SERVICE_BUND_NAME,SERVICE_FORBACK_NAME));
        button6.setClickedListener(component -> stopServerce(SERVICE_BUND_NAME,SERVICE_FORBACK_NAME));

    }
    private Intent getServiceIntent(String bundName,String serviceName) {
        Intent it = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName(bundName)
                .withAbilityName(serviceName)
                .build();
        it.setOperation(operation);
        return it;
    }

    // 开启servece
    private void startServerce(String bundName,String serviceName) {
        Intent it = getServiceIntent(bundName,serviceName);
        startAbility(it);
    }
// 关闭servece
    private void stopServerce(String bundName,String serviceName) {
        Intent it = getServiceIntent(bundName,serviceName);
        stopAbility(it);
    }
    //连接服务需要传入IAbilityConnection的实例
    private IAbilityConnection connection = new IAbilityConnection() {
        @Override
        public void onAbilityConnectDone(ElementName elementName, IRemoteObject iRemoteObject, int i) {
            new ToastDialog(MainAbilitySlice.this).setText("onAbilityConnectDone").show();
        }
        @Override
        public void onAbilityDisconnectDone(ElementName elementName, int i) {
            new ToastDialog(MainAbilitySlice.this).setText("onAbilityDisconnectDone").show();
        }
    };
// 连接servece
    private void connectServerce(String bundName,String serviceName) {
        Intent it = getServiceIntent(bundName,serviceName);
        connectAbility(it,connection);
    }


    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
